import { AdsorbType, ClassName, Events, LinkMark, LinkStyle, Shape } from "@/Constants";
import Link from "@/features/basic-shape/link/Link";
import { Utils } from "../../../utils";
import { IRelativePos } from "../../../Interface";
import Feature from "../../Feature"
import RCtrlPnt from "./RCtrlPnt";
import GridSystem from "@/GridSystem";

// 锚点连线控制点
class ACtrlPnt extends RCtrlPnt {

    links: Link[];
    onlyOneLink = false;   // 只能有一个link

    constructor(parent: Feature, fn: () => IRelativePos, width = 20, height?: number) {
        super(parent, fn, width, height);
        this.className = ClassName.ACTRLPNT;
        this.adsorbTypes = [AdsorbType.POINT];
        this.fillStyle = this.hoverStyle = this.focusStyle = "#73A298";
        this.strokeStyle = '#4B726A'
        this.isStroke = true;
        this.shape = Shape.CIRCLE;
        this.links = [];
        this.onlyOneLink = true;
        this.cbDrag = false;
        if (GridSystem.isMobile) {
            this.on(Events.TOUCHSTART, this.mouseDown);
        } else {
            this.on(Events.MOUSE_DOWN, this.mouseDown);
        }
    }

    mouseDown = (e: any) => {
        if (this.onlyOneLink) Feature.Gls.removeFeature(this.links[this.links.length - 1])
        const link = new Link(this, this);
        link._linkStyle = LinkStyle.DEFAULT;
        this.links.push(link);
        Feature.Gls.addFeature(link);
        if (GridSystem.isMobile) {
            document.addEventListener(Events.TOUCHMOVE, this.mouseMove, { passive: false })
            document.addEventListener(Events.TOUCHEND, this.mouseUp)
        } else {
            document.addEventListener(Events.MOUSE_MOVE, this.mouseMove)
            document.addEventListener(Events.MOUSE_UP, this.mouseUp)
        }
    }

    mouseMove = (e: any) => {
        const movePos = Feature.Gls.getRelativePos(Utils.getMousePos(Feature.Gls.domElement, e));
        const { x: offsetX, y: offsetY } = Feature.Gls.getAdsorb2Point(movePos);
        const link = this.links[this.links.length - 1]
        movePos.x += offsetX;
        movePos.y += offsetY;
        link.setTarget(movePos, LinkMark.END)
    }

    mouseUp = (e: any) => {
        const link = this.links[this.links.length - 1]
        Feature.Gls.setCurrentPos(e);
        Feature.Gls.draw();
        const anchor = [...Feature.Gls.features].reverse().find(f => f.isPointIn && f instanceof ACtrlPnt && f != this);
        if (anchor) link.setTarget(anchor, LinkMark.END);
        if (GridSystem.isMobile) {
            document.removeEventListener(Events.TOUCHMOVE, this.mouseMove)
            document.removeEventListener(Events.TOUCHEND, this.mouseUp)
        } else {
            document.removeEventListener(Events.MOUSE_MOVE, this.mouseMove)
            document.removeEventListener(Events.MOUSE_UP, this.mouseUp)
        }
    }

    destroy(): void {
        super.destroy();
        this.off(Events.TOUCHSTART, this.mouseDown);
        this.off(Events.MOUSE_DOWN, this.mouseDown);
        const links = Feature.Gls.features.filter(f => f instanceof Link && (f.startFeature === this || f.endFeature === this));
        links.forEach(lk => {
            Feature.Gls.removeFeature(lk);
        })
    }
}

export default ACtrlPnt;